//得到类名下面的所有class
function getClass(classnames) {
    let classObj = [];//定义数组
    let classInt = 0;//定义数组的下标
    let tags = document.getElementsByTagName("*"); //获取HTML的所有标签
    for (let index = 0; index < tags.length; index++) {
        if (tags[index].nodeType === 1) {//判断节点类型
            if (tags[index].getAttribute("class") === classnames)//判断和需要CLASS名字相同的，并组成一个数组
            {
                classObj[classInt] = tags[index];
                classInt++;
            }
        }
    }
    return classObj;//返回组成的数组
}

/************获取父节点下面对应class的子节点*********/
function getChildrenClass(pare, childName) {
    let pareNode = pare,
        arr = [];
    for (let i = 0; i < pareNode.children.length; i++) {
        if (pareNode.children[i].className === childName) {
            arr.push(arr);
        }
    }
    return arr;
}

/************获取项目根路径***********/
function getRootPath_web() {
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    let curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    let pathName = window.document.location.pathname;
    let pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    let localhostPaht = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/uimcardprj
    let projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    return (localhostPaht + projectName);
}

/************解析url后面的问号对应的数据，例如：http://localhost:8080/photoShareProject/share/rank?kind=day*/
function parseURL(url) {
    let a = document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':', ''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function () {
            let ret = {},
                seg = a.search.replace(/^\?/, '').split('&'),
                len = seg.length, i = 0, s;
            for (; i < len; i++) {
                if (!seg[i]) {
                    continue;
                }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
        hash: a.hash.replace('#', ''),
        path: a.pathname.replace(/^([^\/])/, '/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
        segments: a.pathname.replace(/^\//, '').split('/')
    };
}

//设备检测:移动端还是pc端
let detectMob = function () {
    if (navigator.userAgent.match(/Android/i)
        || navigator.userAgent.match(/webOS/i)
        || navigator.userAgent.match(/iPhone/i)
        || navigator.userAgent.match(/iPad/i)
        || navigator.userAgent.match(/iPod/i)
        || navigator.userAgent.match(/BlackBerry/i)
        || navigator.userAgent.match(/Windows Phone/i)) {
        return true;
    }
    else {
        return false;
    }
};
/*************** 基础ajax****************/
let ajax_base = function (data, url, method, type, func) {
    let basePath = getRootPath_web(),
        dat;
    if (method === "GET") {
        dat = data;
    } else if (method === "POST") {
        dat = JSON.stringify(data);
    }
    if (type === "Json") {
        return new Promise((resolve, reject) => {
            $.ajax({
                type: method,
                url: basePath + url,
                dataType: "json",
                contentType: "application/json",
                data: dat,
                success: function (data) {
                    let dat = data;
                    if (func) {
                        dat = func(dat);
                    }
                    resolve(dat);
                },
                error: function (data) {
                    reject(data);
                    throw new error("发生错误")
                }
            });
        })
    }
    else if (type === "notJson") {
        return new Promise((resolve, reject) => {
            $.ajax({
                type: "POST",
                url: basePath + url,
                dataType: "json",
                data: data,
                success: function (da) {
                    if (func) {
                        func(da);
                    }
                    resolve(da);
                },
                error: function (da) {
                    reject(data);
                    throw new error("发生错误")
                }
            });
        })
    }
};

